package net.wanho.personalOffice.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.personalOffice.dao.MailDAO;
import net.wanho.personalOffice.po.InstationMail;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MailDAOImpl extends BaseDAOImpl<InstationMail> implements MailDAO {
    @Override
    public List<InstationMail> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            String sql ="SELECT\n" +
                    "  i.MAIL_ID mailId,\n" +
                    "  i.THEME theme,\n" +
                    "  i.STATUS status,\n" +
                    "  i.REMARK remark,\n" +
                    "  i.CREATE_TIME createTime,\n" +
                    "  i.UPDATE_TIME updateTime,\n" +
                    "  i.CONTENT content,\n" +
                    "  e1.EMPLOYEE_ID \"sender.employeeId\",\n" +
                    "  e1.EMPLOYEE_NAME \"sender.employeeName\",\n" +
                    "  e2.EMPLOYEE_ID \"receiver.employeeId\",\n" +
                    "  e2.EMPLOYEE_NAME \"receiver.employeeName\"\n" +
                    "FROM\n" +
                    "  instation_mail i\n" +
                    "  LEFT JOIN employee e1\n" +
                    "    ON i.SENDER_ID = e1.EMPLOYEE_ID\n" +
                    "  LEFT JOIN employee e2\n" +
                    "    ON i.RECEIVER_ID = e2.EMPLOYEE_ID\n" +
                    " WHERE 1=1 ";
            StringBuffer sb = dySQL(condMap,sql);
            Object[] params = {};
            return super.queryForList(sb.toString(),params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    private StringBuffer dySQL(Map<String, String> condMap, String sql) {
        StringBuffer sb = new StringBuffer(sql) ;
        String receiverId = condMap.get("receiverId");
        //根据类别id 精确查询
        if (StringUtils.isNotEmpty(receiverId)){
            sb.append(" AND i.RECEIVER_ID=").append(Integer.parseInt(receiverId)) ;
        }
        return  sb;
    }

    @Override
    public boolean insert(InstationMail entity) throws DAOException {
        try {
            String sql ="INSERT INTO instation_mail (\n" +
                    "  SENDER_ID,\n" +
                    "  RECEIVER_ID,\n" +
                    "  THEME,\n" +
                    "  STATUS,\n" +
                    "  REMARK,\n" +
                    "  CREATE_TIME,\n" +
                    "  UPDATE_TIME,\n" +
                    "  CONTENT\n" +
                    ")\n" +
                    "VALUES\n" +
                    "  (?, ?, ?, ?, ?, NOW(), NOW(), ?);\n";
            Object[] params = {
                    entity.getSender().getEmployeeId(),
                    entity.getReceiver().getEmployeeId(),
                    entity.getTheme(),
                    entity.getStatus(),
                    entity.getRemark(),
                    entity.getContent()
            };
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }

    }
}
